//
//  CarModelVC.m
//  New_Runway
//
//  Created by mack on 2018/2/2.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarModelVC.h"
#import "CarSeriesModel.h"
#import "CarSeriesVC.h"

@interface CarModelVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
//@property (nonatomic, assign)NSInteger selIndex;//上一次选中的索引
@property (nonatomic, strong) NSIndexPath *firstSele;

@end

@implementation CarModelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"车系";
    self.view.backgroundColor= kColorWhite();
    [self initTableview];
    [self loadCarSeries:self.carBrandId];
    self.firstSele = self.modelSeleIndexPath;
    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    //    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    self.tableview.tableFooterView = [[UIView alloc] init];

    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.seriesArray.count + 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.tintColor = MAIN_NAV_COLOR();

    cell.textLabel.textColor = MAIN_FONT_COLOR();
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];

    if (indexPath.row == 0) {
        cell.textLabel.text = @"不限车系";
    }else{
    seriesResult *seriesInfo = self.seriesArray[indexPath.row - 1];
    cell.textLabel.text = seriesInfo.seriesName;
    }
    if (self.modelSeleIndexPath == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = MAIN_NAV_COLOR();

    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self cellDidClickWithIndex:indexPath];

}

- (void)cellDidClickWithIndex:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        NSDictionary *dic = @{
                              @"brandId":@(self.carBrandId),
                              @"modelId":@"",
                              @"seriesId":@"",
                              @"brandName": self.carBrandName
                              };
        NSNotification *notificatiom;
        if (self.isDesk == YES) {
          notificatiom  = [NSNotification notificationWithName:@"BrandSearchDesk" object:nil userInfo:dic];
        }else{
          notificatiom = [NSNotification notificationWithName:@"BrandSearch" object:nil userInfo:dic];
        }
        [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
        
        NSDictionary *indexoathDic = @{
                                       @"brandIndexPath":self.brandIndexPath,
                                       @"modelIndexPath":[NSIndexPath indexPathForRow:0 inSection:0],
                                       @"seriesIndexPath":[NSIndexPath indexPathForRow:0 inSection:0]
                                       };
        NSNotification *selectedIndexPath;
        if (self.isDesk == YES) {
            selectedIndexPath = [NSNotification notificationWithName:@"SELEINDEXPATHDESK" object:nil userInfo:indexoathDic];
        }else{
            selectedIndexPath = [NSNotification notificationWithName:@"SELEINDEXPATH" object:nil userInfo:indexoathDic];
        }
        [[NSNotificationCenter defaultCenter] postNotification:selectedIndexPath];
        
        
        if (self.isDesk == YES) {
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
            
        } else {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }else{
        seriesResult *seriesInfo = self.seriesArray[indexPath.row - 1];
        CarSeriesVC *modelVC = [[CarSeriesVC alloc] init];
        modelVC.modelId = seriesInfo.seriesId;
        modelVC.carBrandId = self.carBrandId;
        modelVC.carBrandName = self.carBrandName;
        modelVC.modelName = seriesInfo.seriesName;
        modelVC.isDesk = self.isDesk;
        modelVC.modelIndexPath = indexPath;
        modelVC.brandIndexPath = self.brandIndexPath;
        if (self.firstSele == indexPath) {
            modelVC.seriesIndexPath = self.seriesSeleIndexPath;
        }else{
            modelVC.seriesIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        }
        
        [self.navigationController pushViewController:modelVC animated:YES];
    }
    self.modelSeleIndexPath = indexPath;
    [self.tableview reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadCarSeries:(NSInteger)brandId{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,carSeriesApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"brandId":@(brandId)} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CarSeriesModel *model = [CarSeriesModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.seriesArray removeAllObjects];
            for (NSDictionary *dic in model.result) {
                seriesResult *seriesInfo = [seriesResult mj_objectWithKeyValues:dic];
                [self.seriesArray addObject:seriesInfo];
            }
            
        }
        [self.tableview reloadData];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}
-(NSMutableArray *)seriesArray{
    if (!_seriesArray) {
        _seriesArray = [[NSMutableArray alloc] init];
    }
    return _seriesArray;
    
}
@end
